#include "capture.h"
#include "onu_comm.h"
#include "dial.h"
#include "ghttp.h"

extern thread_pool_t *Tpool;
static char Url[32] = "http://www.baidu.com/";

static OPT_T opts[] = {
    {"--http-url", Url, OPT_STRING, "http url"},
};

float *Valid_bytes_ptr = NULL;
int Valid_bytes_count = 0;

// void show_cur_speed(void *cur_speed){
//     char bps_str[32];
//     bps2str(*(float*)cur_speed, bps_str);
//     log_debug("cur_speed:%s", bps_str);
// }
// void *show_rt_bytes(void *arg){
//     int i =0, count = 15;
//     char _str[32];
//     while(count--){
//         for(i =0; i < RT_Bytes_len; i++){
//             byte2str(RT_Bytes[i], _str);
//             log_debug("idx[%d] bytes:%s", i, _str);
//         }
//         sleep(1);
//     }
//     return NULL;
// }
void* http_upload_task(void *arg){
    dial_arg_t dial_arg = { 0 };
    dial_result_t* dial_result;
    char *urls[] = {
        "http://222.221.16.187:7083/sts-base-node/servlet/upLoadData"
    };
    // dial_arg.in.http_arg.uri = "http://10.0.0.210:18081/sts-base-node/servlet/upLoadData";
    dial_arg.in.http_arg.uris = urls;
    dial_arg.in.http_arg.uris_count = Array_Size(urls);
    dial_arg.in.http_arg.threads = 1;
    dial_arg.in.http_arg.duration = 15000;
    dial_arg.in.http_arg.interval = 1000;
    dial_arg.in.http_arg.ignore_duration = 5000;
    Valid_bytes_count = dial_arg.in.http_arg.threads;
    Valid_bytes_ptr = calloc(Valid_bytes_count, sizeof(float));
    // dial_arg.in.http_arg.callback = show_cur_speed;  
    dial_result = (dial_result_t*)dial_http_upload(&dial_arg);
    http_result_print(&dial_result->out.http_result);
    free(Valid_bytes_ptr);
    free(dial_result);

    return NULL;
}
void* http_download_task(void *arg){
    int count = 0;
    dial_arg_t dial_arg = { 0 };
    dial_result_t* dial_result;
    char *urls[] = {
        // "http://10.0.0.222:9092/nodeFile/test.bin",
        // "http://112.194.200.117:8090/nodeFile/test.bin",
        // "http://10.0.0.210:18081/nodeFile/test.bin",
        // "http://10.0.0.200:9092/nodeFile/test.bin",
        // "http://download.pplive.com/pptvsetup_4.0.5.0069.exe",
        "http://222.221.16.187:7083/nodeFile/test.bin"
    };
    while(1){
        log_debug("\n\n count:%d", count++);
        bzero(&dial_arg, sizeof(dial_arg));
        dial_arg.in.http_arg.uris = urls;
        dial_arg.in.http_arg.uris_count = Array_Size(urls);
        dial_arg.in.http_arg.threads = 20;
        Valid_bytes_count = dial_arg.in.http_arg.threads;
        Valid_bytes_ptr = calloc(Valid_bytes_count, sizeof(float));
        dial_arg.in.http_arg.valid_bytes_ptr = Valid_bytes_ptr;
        dial_arg.in.http_arg.duration = 15000;
        dial_arg.in.http_arg.interval = 1000;
        dial_arg.in.http_arg.ignore_duration = 5000;   
        // dial_arg.in.http_arg.callback = show_cur_speed;   
        dial_result = (dial_result_t*)dial_http_download(&dial_arg);
        http_result_print(&dial_result->out.http_result);
        free(Valid_bytes_ptr);
        free(dial_result);
    }
    

    return NULL;
}
void* http_task(void* arg)
{
    int i =0;
    // for ( i = 0; i < 100; ++i)
    while(1)
    {
        dial_arg_t dial_arg = { 0 };
        dial_result_t* dial_result;

        //dial_arg.bridge_mode = 1;
        dial_arg.in.http_arg.uri = Url;
        dial_arg.in.http_arg.timeout = 3;
        dial_arg.in.http_arg.duration = 0;
        dial_result = (dial_result_t*)dial_http_light(&dial_arg);
        http_result_print(&dial_result->out.http_result);
        if(dial_result->err != 0 ){
            break;
        }
        free(dial_result);

        // sleep(2);
    }
    

    return NULL;
}

void *ghttp_task(void *arg){
    int i = 0;
    for ( i = 0; i < 100; ++i)
    {
        ghttp_request *req = ghttp_request_new();

        ghttp_set_type(req, ghttp_type_get);
        ghttp_set_uri(req, Url);
        ghttp_prepare(req);
        ghttp_process(req);

        log_trace("code:%d", ghttp_status_code(req));
        log_trace("body len:%d", ghttp_get_body_len(req));
        // log_trace("body:%s" ghttp_get_body(req));
        ghttp_request_destroy(req);
    }
    
}

int http_task_init(int argc, char** argv)
{
    opt_parse(opts, sizeof(opts), argc, argv);

    // thread_pool_add_task(Tpool, ghttp_task, NULL);
    thread_pool_add_task(Tpool, http_task, NULL);
    // thread_pool_add_task(Tpool, http_upload_task, NULL);
    // thread_pool_add_task(Tpool, http_download_task, NULL);
    // thread_pool_add_task(Tpool, show_rt_bytes, NULL);
    return 0;
}
